package com.mlizhi.modules.spec.content;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mlizhi.kingdom.R;
import com.mlizhi.utils.Constants;
import com.umeng.analytics.MobclickAgent;

public class SpecContentViewActivity extends Activity {
	//

	private ImageView backImg = null;
	private WebView mWebView;
	private TextView contentTitleTextView ;
	private ProgressBar pb; 
	
	
	 private static final String insertJavaScript = "javascript:window.onload=function(){var imgs = document.getElementsByTagName(\"img\");for(var i=0;i<imgs.length;i++){imgs[i].style.width=\"100%\";}}";  
	
	@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_spec_content_view);
		this.backImg = (ImageView) findViewById(R.id.id_spec_content_view_title_back);
		this.backImg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SpecContentViewActivity.this.finish();
			}
		});
		
		Intent intent = getIntent();
//		String contentId = intent.getStringExtra(Constants.CONTENT_ITEM_ID);
//		String contentTitle = intent.getStringExtra(Constants.CONTENT_ITEM_TITLE);
		String contentUrl = intent.getStringExtra(Constants.CONTENT_ITEM_URL);
		mWebView = (WebView) findViewById(R.id.id_spec_setting_content_webview);
    	pb = (ProgressBar) findViewById(R.id.id_spec_content_view_pb);  
        pb.setMax(100);  
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setSaveEnabled(true);
		mWebView.setWebViewClient(new SampleWebViewClient());
		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				pb.setProgress(newProgress);
				if (newProgress == 100) {
					pb.setVisibility(View.GONE);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
		mWebView.loadUrl(contentUrl);
//		mWebView.loadUrl(Constants.URL_CONTENT_DETAIL_URL+contentId);
//		contentTitleTextView = (TextView) findViewById(R.id.id_spec_content_view_title_tv);
//		if(contentTitle.length()>11) contentTitle = contentTitle.substring(0,10)+"……";
//		contentTitleTextView.setText(contentTitle);

	}
	private static class SampleWebViewClient extends WebViewClient {
		
		 @Override  
		 public void onPageStarted(WebView view, String url, Bitmap favicon) {  
			 super.onPageStarted(view, url, favicon);  
			 //注入  
			 view.loadUrl(insertJavaScript);  
		 }  
		 
		 @Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
			view.stopLoading();
//			String data = "<center><table width=\"100%\" height=\"100%\"><tr height=\"100%\"> <td vertical=\"middle\">Page NO FOUND！</td></tr></table></center>"; 
//			view.setBackgroundResource(R.drawable.ic_theme_background);
//			view.loadUrl("javascript:document.body.innerHTML=\"" + data + "\""); 
			view.loadUrl("file:///android_asset/404.html"); 
			
		}
		 
		 
		 
	}
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SplashScreen"); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
	    MobclickAgent.onResume(this);          //统计时长
	}

	@Override
	public void onPause() {
		super.onPause();
		 MobclickAgent.onPageEnd("SplashScreen"); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息 
		    MobclickAgent.onPause(this);
	}

}
